IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Evaluations_EvaluationSources]') AND parent_object_id = OBJECT_ID(N'[dbo].[Evaluations]'))
ALTER TABLE [dbo].[Evaluations] DROP CONSTRAINT [FK_Evaluations_EvaluationSources]
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Evaluations_RatingTypes]') AND parent_object_id = OBJECT_ID(N'[dbo].[Evaluations]'))
ALTER TABLE [dbo].[Evaluations] DROP CONSTRAINT [FK_Evaluations_RatingTypes]
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Evaluations_Recommendations]') AND parent_object_id = OBJECT_ID(N'[dbo].[Evaluations]'))
ALTER TABLE [dbo].[Evaluations] DROP CONSTRAINT [FK_Evaluations_Recommendations]
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Companies_Users]') AND parent_object_id = OBJECT_ID(N'[dbo].[Companies]'))
ALTER TABLE [dbo].[Companies] DROP CONSTRAINT [FK_Companies_Users]
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Recommendations_Companies]') AND parent_object_id = OBJECT_ID(N'[dbo].[Recommendations]'))
ALTER TABLE [dbo].[Recommendations] DROP CONSTRAINT [FK_Recommendations_Companies]
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Recommendations_RatingTypes]') AND parent_object_id = OBJECT_ID(N'[dbo].[Recommendations]'))
ALTER TABLE [dbo].[Recommendations] DROP CONSTRAINT [FK_Recommendations_RatingTypes]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Keywords]') AND type in (N'U'))
DROP TABLE [dbo].[Keywords]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Evaluations]') AND type in (N'U'))
DROP TABLE [dbo].[Evaluations]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Users]') AND type in (N'U'))
DROP TABLE [dbo].[Users]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RatingTypes]') AND type in (N'U'))
DROP TABLE [dbo].[RatingTypes]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Companies]') AND type in (N'U'))
DROP TABLE [dbo].[Companies]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Recommendations]') AND type in (N'U'))
DROP TABLE [dbo].[Recommendations]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EvaluationSources]') AND type in (N'U'))
DROP TABLE [dbo].[EvaluationSources]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Users]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Users](
	[UserID] [int] IDENTITY(1,1)  NOT NULL,
	[UserName] [varchar](20) NOT NULL,
	[Password] [varchar](16) NOT NULL,
	[FullName] [varchar](60) NOT NULL,
	[UserType] [int] NOT NULL,
 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
	[UserID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RatingTypes]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[RatingTypes](
	[RatingTypeId] [int] IDENTITY(1,1)  NOT NULL,
	[Name] [varchar](32) NOT NULL,
	[Value] [tinyint] NOT NULL,
 CONSTRAINT [PK_RatingTypes] PRIMARY KEY CLUSTERED 
(
	[RatingTypeId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EvaluationSources]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[EvaluationSources](
	[EvaluationSourceID] [int] IDENTITY(1,1)  NOT NULL,
	[Name] [varchar](60) NULL,
	[URL] [varchar](500) NOT NULL,
 CONSTRAINT [PK_EvaluationSources] PRIMARY KEY CLUSTERED 
(
	[EvaluationSourceID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Keywords]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Keywords](
	[KeyWordID] [int] IDENTITY(1,1)  NOT NULL,
	[KeyWord] [varchar](30) NOT NULL,
	[Weight] [tinyint] NOT NULL,
 CONSTRAINT [PK_Keywords] PRIMARY KEY CLUSTERED 
(
	[KeyWordID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Recommendations]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Recommendations](
	[RecommendationID] [int] IDENTITY(1,1)  NOT NULL,
	[CompanyID] [int] NOT NULL,
	[RatingTypeID] [int] NOT NULL,
	[RecommendationDate] [smalldatetime] NOT NULL,
	[Rating] [int] NOT NULL,
	[Price] [smallmoney] NULL,
 CONSTRAINT [PK_Recommendations] PRIMARY KEY CLUSTERED 
(
	[RecommendationID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Companies]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Companies](
	[CompanyID] [int] IDENTITY(1,1)  NOT NULL,
	[UserID] [int] NOT NULL,
	[CompanyName] [varchar](30) NOT NULL,
	[URL] [varchar](500) NULL,
	[Symbol] [varchar](8) NOT NULL,
	[Exchange] [varchar](20) NOT NULL,
 CONSTRAINT [PK_Companies] PRIMARY KEY CLUSTERED 
(
	[CompanyID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Evaluations]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Evaluations](
	[EvaluationID] [int] IDENTITY(1,1)  NOT NULL,
	[EvaluationSourceID] [int] NOT NULL,
	[RecommendationID] [int] NULL,
	[RatingTypeID] [int] NULL,
	[Description] [varchar](max) NOT NULL,
	[URL] [varchar](500) NOT NULL,
	[EvaluationDate] [smalldatetime] NOT NULL,
 CONSTRAINT [PK_Evaluations] PRIMARY KEY CLUSTERED 
(
	[EvaluationID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Recommendations_Companies]') AND parent_object_id = OBJECT_ID(N'[dbo].[Recommendations]'))
ALTER TABLE [dbo].[Recommendations]  WITH CHECK ADD  CONSTRAINT [FK_Recommendations_Companies] FOREIGN KEY([CompanyID])
REFERENCES [dbo].[Companies] ([CompanyID])
GO
ALTER TABLE [dbo].[Recommendations] CHECK CONSTRAINT [FK_Recommendations_Companies]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Recommendations_RatingTypes]') AND parent_object_id = OBJECT_ID(N'[dbo].[Recommendations]'))
ALTER TABLE [dbo].[Recommendations]  WITH CHECK ADD  CONSTRAINT [FK_Recommendations_RatingTypes] FOREIGN KEY([RatingTypeID])
REFERENCES [dbo].[RatingTypes] ([RatingTypeId])
GO
ALTER TABLE [dbo].[Recommendations] CHECK CONSTRAINT [FK_Recommendations_RatingTypes]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Companies_Users]') AND parent_object_id = OBJECT_ID(N'[dbo].[Companies]'))
ALTER TABLE [dbo].[Companies]  WITH CHECK ADD  CONSTRAINT [FK_Companies_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[Users] ([UserID])
GO
ALTER TABLE [dbo].[Companies] CHECK CONSTRAINT [FK_Companies_Users]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Evaluations_EvaluationSources]') AND parent_object_id = OBJECT_ID(N'[dbo].[Evaluations]'))
ALTER TABLE [dbo].[Evaluations]  WITH CHECK ADD  CONSTRAINT [FK_Evaluations_EvaluationSources] FOREIGN KEY([EvaluationSourceID])
REFERENCES [dbo].[EvaluationSources] ([EvaluationSourceID])
GO
ALTER TABLE [dbo].[Evaluations] CHECK CONSTRAINT [FK_Evaluations_EvaluationSources]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Evaluations_RatingTypes]') AND parent_object_id = OBJECT_ID(N'[dbo].[Evaluations]'))
ALTER TABLE [dbo].[Evaluations]  WITH CHECK ADD  CONSTRAINT [FK_Evaluations_RatingTypes] FOREIGN KEY([RatingTypeID])
REFERENCES [dbo].[RatingTypes] ([RatingTypeId])
GO
ALTER TABLE [dbo].[Evaluations] CHECK CONSTRAINT [FK_Evaluations_RatingTypes]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Evaluations_Recommendations]') AND parent_object_id = OBJECT_ID(N'[dbo].[Evaluations]'))
ALTER TABLE [dbo].[Evaluations]  WITH CHECK ADD  CONSTRAINT [FK_Evaluations_Recommendations] FOREIGN KEY([RecommendationID])
REFERENCES [dbo].[Recommendations] ([RecommendationID])
GO
ALTER TABLE [dbo].[Evaluations] CHECK CONSTRAINT [FK_Evaluations_Recommendations]
 